/**
 * Created by Joe on 2017/7/5 0005.
 */
var Invoice = {
    fields: {
        selects: [], //下拉框
        radios: [],//单选框
        checkboxs: [],//复选款
        textareas: [],//多行文本框
        selfDefine: []
    },
    current_row: null
};
$(document).ready(function () {

    $('#DataTables_partner_invoice').DataTable({
        "paging": true,
        "searching": true,
        "info": true,
        "ordering": true,
        "language": {
            "url": "../vendor/datatables/zh-cn.json"
        }
    });
    $('#DataTables_bill_list').DataTable({
        "paging": false,
        "searching": false,
        "info": false,
        "ordering": false,
        "language": {
            "url": "../vendor/datatables/zh-cn.json"
        }
    });

    $('#btn-create-invoice').on('click', function () {
        $("#new-invoice-modal").modal("toggle");
    });

    $('#btn-query').on('click', function () {
        requestQueryInvoice();
    });
    $('#save-new-invoice').on('click', function () {
        requestCreateInvoice();
    });
    $('#saven-update-invoice').on('click', function () {
        requestUpdateInvoice();
    });

    $('#DataTables_partner_invoice').on('click', '.btn-detail', function () {
        var $tr = $(this).parents('tr');
        var row = $('#DataTables_partner_invoice').DataTable().row($tr);
        Invoice.current_row = row;
        requestGetDetail($(this).siblings("input").val());
    });

    $('#DataTables_partner_invoice').on('click', '.btn-delete', function () {
        var $tr = $(this).parents('tr');
        var row = $('#DataTables_partner_invoice').DataTable().row($tr);
        var url = /*[[@{/invoice/removeBy}]]*/ '/invoice/removeBy';
        Utils.ajaxSubmit(url + "/" + $(this).siblings("input").val(), {}, function (data) {
            if (data.result == "success") {
                row.remove().draw();
                layer.msg('发票信息已删除', {time: 3000, icon: 6});
            }
            if (data.result == "fail") {
                Utils.errorAlert(data.result.errorMessage);
            }
        });
    });
    /* 城市切换 */
    $('#selectedCompany').change(function () {
        var companyName = $("#selectedCompany").find("option:selected").text();
        $("#new-invoice-form").find("input[name='companyName']").val(companyName);
    });
    $("#ddlregtype").find("option:selected").text();

});

function requestQueryInvoice() {
    $("#DataTables_partner_invoice").DataTable().rows().remove().draw();
    var companyId = $("#companyId").val();
    var url = /*[[@{/invoice/queryBy}]]*/ '/invoice/queryBy';
    Utils.ajaxSubmit(url + "/" + companyId, {}, function (data) {
        // console.log("data ： ", data);
        if (data.result == "success") {
            addTableRow(data.content, "#DataTables_partner_invoice");
        }
        if (data.result == "fail") {
            Utils.errorAlert(data.errorMessage);
        }
    });
}
function requestCreateInvoice() {
    var url = /*[[@{/invoice/createCompanyInvoice}]]*/ '/invoice/createCompanyInvoice';
    var params = $("#new-invoice-form").serialize();
    Utils.ajaxSubmit(url, params, function (data) {
        // console.log("data ： ", data);
        if (data.result == "success") {
            $("#new-invoice-modal").modal("toggle");
            addTableSingleRow(data.content, "#DataTables_partner_invoice");
        }
        if (data.result == "fail") {
            Utils.errorAlert(data.errorMessage);
        }
    });
}
function requestUpdateInvoice() {
    var url = /*[[@{/invoice/updateCompanyInvoice}]]*/ '/invoice/updateCompanyInvoice';
    var params = $("#invoice-form").serialize();
    Utils.ajaxSubmit(url, params, function (data) {
        // console.log("data ： ", data);
        if (data.result == "success") {
            $("#invoice-detail-modal").modal("toggle");
            Invoice.current_row.remove().draw();
            addTableSingleRow(data.content, '#DataTables_partner_invoice');
        }
        if (data.result == "fail") {
            Utils.errorAlert(data.errorMessage);
        }
    });
}
function requestGetDetail(invoiceId) {
    var url = /*[[@{/invoice/queryInvoiceById}]]*/ '/invoice/queryInvoiceById';
    Utils.ajaxSubmit(url + "/" + invoiceId, {}, function (data) {
        // console.log("data ： ", data);
        if (data.result == "success") {
            Utils.fillForms(data.content, Invoice.fields);
            $("#DataTables_bill_list").DataTable().rows().remove().draw();
            addBillListTableRow(data.content.invoiceBillDtos, "#DataTables_bill_list")
            $("#invoice-detail-modal").modal("toggle");
        }
        if (data.result == "fail") {
            Utils.errorAlert(data.errorMessage);
        }
    });
}
function addBillListTableRow(invoiceBillList, targetEle) {
    $.each(invoiceBillList, function (k, v) {
        $(targetEle).dataTable().fnAddData([v.accountBillNo, v.amount]);
    });
}
/**添加table行*/
function addTableRow(invoiceList, targetEle) {
    $.each(invoiceList, function (k, v) {
        addTableSingleRow(v, targetEle);
    });
}
function addTableSingleRow(v, targetEle) {
    var btnRemoveEle = '<input type="hidden" value="' + v.id + '"> <a class="btn btn-primary btn-detail" >查看明细</a>';
    btnRemoveEle = btnRemoveEle.concat('&nbsp;&nbsp;&nbsp;<a class="btn btn-danger btn-delete">删除</a>');
    if (v.invoiceType == '1') {
        v.invoiceType = '普通发票';
    }
    if (v.invoiceType == '2') {
        v.invoiceType = '增值税发票';
    }
    $(targetEle).dataTable().fnAddData([v.invoiceNo, v.invoiceType, v.taxSerialNum, v.title, v.bank, v.bankAccount, btnRemoveEle]);
}


